package com.group_e.Bubble.client;

//import javax.servlet.http.*;

//import com.google.appengine.api.datastore.Query;
//import com.google.appengine.api.rdbms.AppEngineDriver;
import java.sql.*;

public class Task_Operate_D 
{
	private Connection con;
	private String User_ID;

	public Task_Operate_D(Connection con, String User_ID) 
	{
		this.con = con;
		this.User_ID = User_ID;
	}
	
	// for query all tasks of one same user_id
	// return the TASK_ID in ResultSet type
	public ResultSet queryAllTask( ) throws SQLException 
	{
		String sql = "SELECT TASK_ID FROM Task WHERE USER_ID = "+User_ID+";";

		ResultSet rs = null;
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
		}
		catch (SQLException e) {
	        e.printStackTrace();
	    }
		finally {
			if (stmt != null) {
				stmt.close();
				}
			}
		
		return rs;		
	}
	
	// for add an entry
	public ResultSet addTask(Task_D tk) throws SQLException
	{
		StringBuilder sql = new StringBuilder();
		
		  sql.append("INSERT INTO Task ( ")
		  		.append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("?, ")
		        .append("? ); ");

		ResultSet rs = null;
		PreparedStatement pstmt = null;
		try {
			   pstmt = con.prepareStatement(sql.toString());
			   pstmt.setString(1, tk.getUser().toString());
			   pstmt.setString(2, tk.getID().toString());
			   pstmt.setString(3, tk.getName());
			   pstmt.setString(4, tk.getDate().toString());
			   pstmt.setString(5, tk.getDsp());
			   pstmt.setString(6, tk.getDueday().toString());
			   pstmt.setString(7, tk.getContext());
			rs = pstmt.executeQuery();
		}
		catch (SQLException e) {
	        e.printStackTrace();
	    }
		finally {
				pstmt.close();
				}
		
		return rs;
	}
	
	// for removal of an entry
	public ResultSet removeTask(Long ID) throws SQLException
	{
		String sql = "DELETE * FROM Task WHERE USER_ID = "+User_ID+" AND TASK_ID = "+ID+";";
		ResultSet rs = null;
		Statement stmt = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
		}
		catch (SQLException e) {
	        e.printStackTrace();
	    }
		finally {
			if (stmt != null) {
				stmt.close();
				}
			}
		
		return rs;
	}
	
	
	public void updateTask(Task_D tk) throws SQLException
	{
		  StringBuilder sql = new StringBuilder();
		  sql.append("INSERT INTO TASK ( ")
	  		.append("?, ")
	        .append("?, ")
	        .append("?, ")
	        .append("?, ")
	        .append("?, ")
	        .append("?, ")
	        .append("? ); ");
		  
		  PreparedStatement pstmt = null;
		  
		  try {
		   pstmt = con.prepareStatement(sql.toString());
		   pstmt.setString(1, tk.getUser().toString());
		   pstmt.setString(2, tk.getID().toString());
		   pstmt.setString(3, tk.getName());
		   pstmt.setString(4, tk.getDate().toString());
		   pstmt.setString(5, tk.getDsp());
		   pstmt.setString(6, tk.getDueday().toString());
		   pstmt.setString(7, tk.getContext());
		   
		   pstmt.executeUpdate();
		   }
		  catch(SQLException e) {
		   e.printStackTrace();	}
		  finally {
		   pstmt.close(); }
		  
	}
	
}
